//
//  QsbkOnlyContextTableViewCell.m
//  NewProject
//
//  Created by Livespro on 2017/5/2.
//  Copyright © 2017年 FZ. All rights reserved.
//

#import "QsbkOnlyContextTableViewCell.h"

@interface QsbkOnlyContextTableViewCell ()

@property (nonatomic, strong)  UIImageView *headerImg;

@property (nonatomic, strong)  UILabel *headerName;

@property (nonatomic, strong)  UILabel *contextLabel;

@property (nonatomic, strong)  UIImageView *contextImg;

@property (nonatomic, strong)  UIButton *video_Btn;

@end

@implementation QsbkOnlyContextTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self.contentView addSubview:self.headerView];
        
        [self.contentView addSubview:self.contextView];
        
    }
    return self;
}

- (QsbkContextHeaderView *)headerView{
    
    if (!_headerView) {
        
        _headerView = [[QsbkContextHeaderView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kQsbkHeaderHeight)];
        //头像
        _headerImg = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, kQsbkHeaderHeight - 20, kQsbkHeaderHeight - 20)];
        [_headerImg zy_cornerRadiusRoundingRect];
        //昵称
        _headerName = [UILabel labelWithText:@"" TextFont:12 TextColor:Color666 Frame:CGRectMake(CGRectGetMaxX(_headerImg.frame) + 10, CGRectGetMinY(_headerImg.frame), kScreenWidth - CGRectGetMaxX(_headerImg.frame) *2, CGRectGetHeight(_headerImg.frame)) NumberOfLines:1 TextAlignment:NSTextAlignmentLeft BackgroundColor:[UIColor clearColor]];
        
        [_headerView addSubview:_headerImg];
        [_headerView addSubview:_headerName];
    }
    
    return _headerView;
}

- (QsbkShareView *)shareView{
    
    if (!_shareView) {
        
        _shareView = [[QsbkShareView alloc]init];
    }
    
    return _shareView;
}

- (UIView *)contextView{
    
    if (!_contextView) {
        
        _contextView = [[UIView alloc]initWithFrame:CGRectZero];
        
        _contextLabel = [UILabel labelWithText:@"" TextFont:kQsbkContentFont TextColor:Color666 Frame:CGRectZero NumberOfLines:0 TextAlignment:NSTextAlignmentLeft BackgroundColor:[UIColor clearColor]];
        
        _contextImg = [[UIImageView alloc]initWithFrame:CGRectZero];
        _contextImg.userInteractionEnabled = YES;
        
        [_contextImg addSubview:self.video_Btn];//添加播放按钮
        
        [_contextView addSubview:_contextLabel];
        
        [_contextView addSubview:_contextImg];
        
        _contextImg.tag = 10010;
    }
    return _contextView;
}

//视频播放按钮
- (UIButton *)video_Btn{
    
    if (!_video_Btn) {
        
        _video_Btn = [UIButton buttonWithCustomTypeNomalImage:@"video_icon" Frame:CGRectMake(0, 0, 35, 35)];
        
        __weak typeof(self) weakSelf = self;
        [[_video_Btn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
            
            if (weakSelf.videoCallBack) {
                
                weakSelf.videoCallBack();
            }
        }];
        
    }
    return _video_Btn;
}

//赋值
- (void)setModel:(QsbkOnlyContextModel *)model{
    _model = model;
    
    [_headerImg yy_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http:%@",_model.user.thumb]] options:YYWebImageOptionShowNetworkActivity];
    
    _headerName.text = _model.user.login;
    
    //内容
    self.contextLabel.text = _model.content;
    
    NSURL *picUrl;
    if ([model.format isEqualToString:@"video"] || [model.format isEqualToString:@"image"]) {
        
        picUrl = [NSURL URLWithString: [model.format isEqualToString:@"video"] ? _model.pic_url :[NSString stringWithFormat:@"http:%@",_model.low_loc]];
        
    }
    
    [self.contextImg yy_setImageWithURL:picUrl options:YYWebImageOptionShowNetworkActivity];
    
    self.video_Btn.hidden = [model.format isEqualToString:@"video"] ? NO : YES;

}

- (void)layoutSubviews{
    
    self.contextView.frame = CGRectMake(0, kQsbkHeaderHeight, kScreenWidth, _model.contentHeight + 10 + _model.imageHeight);
    
    self.contextLabel.frame = CGRectMake(10, 0, kScreenWidth - 20, _model.contentHeight);
    
    self.contextImg.frame = CGRectMake(10, CGRectGetMaxY(self.contextLabel.frame) +10, kScreenWidth - 20, _model.imageHeight);
    
    self.video_Btn.center = CGPointMake((kScreenWidth - 20)/2, _model.imageHeight/2);
}

@end
